package com.gm.wholesale.module.order.view;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.gm.commonlib.views.popupwindow.Popupwindow;
import com.gm.commonlib.views.superview.SuperTextView;
import com.gm.commonlib.views.toast.ToastUtil;
import com.gm.wholesale.R;
import com.gm.wholesale.module.good.bean.GoodBean;
import com.gm.wholesale.module.main.ui.MainActivity;
import com.gm.wholesale.module.voice.voiceCreateOrder.VoiceCreateOrderManger;

public class VoiceInputView extends LinearLayout {
    private Context mContext;
    private TextView text1;
    private TextView goodNameTv;
    private ImageView voiceImage;
    private SuperTextView cancelTv;
    private AnimationDrawable drawable;//语音播放动画

    private MainActivity mActivity;

    public VoiceInputView(Context context) {
        super(context);
        initView(context);
    }

    public VoiceInputView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        mActivity = (MainActivity) context;
        mContext = context;
        LayoutInflater.from(context).inflate(R.layout.view_voice_input, this);
        text1 = findViewById(R.id.text1);
        goodNameTv = findViewById(R.id.goodNameTv);
        voiceImage = findViewById(R.id.voiceImage);
        cancelTv = findViewById(R.id.cancelTv);
        drawable = (AnimationDrawable) voiceImage.getBackground();
        cancelTv.setOnClickListener(view -> {
            VoiceCreateOrderManger.newInstance().stop();
            Popupwindow.hiddenPop();
        });
        playAnim();

    }

    public void startVoice(){
        startVoiceCreateOrder();
    }
    public void startVoiceCreateOrder(){
        VoiceCreateOrderManger.newInstance().start(mContext, new VoiceCreateOrderManger.OnVoiceCreateOrderListener() {
            @Override
            public void createOrder(GoodBean goodBean) {
                if (mActivity.mCreateOrderUiPre==null)return;
                mActivity.mCreateOrderUiPre.createOrder(goodBean);
            }

            @Override
            public void showCreateOrderView(GoodBean goodBean) {
                if (mActivity.mCreateOrderUiPre==null)return;
                mActivity.mCreateOrderUiPre.showCreateOrderViewByVoice(goodBean.getName());
            }

            @Override
            public void onFinish(String text) {
                if (!TextUtils.isEmpty(text)){
                    ToastUtil.showToast(text);
                }

            }


        });


    }


    public void reset() {
        text1.setVisibility(VISIBLE);
        goodNameTv.setText("商品名");
    }




    public void stopAnim() {
        if (drawable != null) {
            drawable.stop();
        }

    }

    public void playAnim() {
        if (drawable != null) {
            drawable.start();
        }
    }
}
